package master.ghrm.domain;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Version;

import org.apache.commons.collections.CollectionUtils;
import org.hibernate.annotations.Type;
import org.joda.time.DateTime;

@Entity
public class EvaluationSession extends AbstractBaseEntity {

	private static final long serialVersionUID = 1L;

	public static final int LENGTH_NAME = 100;

	@Version
	private Long version;

	@Id
	@GeneratedValue(strategy = GenerationType.AUTO)
	@Column(name = "evaluation_session_id")
	private Integer evaluationSessionId;

	@Column(length = LENGTH_NAME)
	private String name;

	@Column(name = "start_date")
	@Type(type = "org.joda.time.contrib.hibernate.PersistentDateTime")
	private DateTime startDate;

	@Column(name = "end_date")
	@Type(type = "org.joda.time.contrib.hibernate.PersistentDateTime")
	private DateTime endDate;

	private Boolean closed;

	@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "evaluationSession")
	private List<Evaluation> evaluations = new ArrayList<Evaluation>();

	/**
	 * Default constructor, persistence use.
	 */
	public EvaluationSession() {
	}

	/**
	 * @param aName name.
	 * @param aStartDate start date.
	 * @param anEndDate end date.
	 */
	public EvaluationSession(String aName, DateTime aStartDate, DateTime anEndDate) {
		super();
		this.name = aName;
		this.startDate = aStartDate;
		this.endDate = anEndDate;
	}

	/**
	 * For persistence use.
	 * 
	 * @return entity version.
	 */
	Long getVersion() {
		return version;
	}

	/**
	 * For persistence use.
	 * 
	 * @param aVersion entity version.
	 */
	void setVersion(Long aVersion) {
		this.version = aVersion;
	}

	/**
	 * @return ID.
	 */
	public Integer getEvaluationSessionId() {
		return evaluationSessionId;
	}

	/**
	 * @param anEvaluationSessionId ID.
	 */
	public void setEvaluationSessionId(Integer anEvaluationSessionId) {
		this.evaluationSessionId = anEvaluationSessionId;
	}

	/**
	 * @return name.
	 */
	public String getName() {
		return name;
	}

	/**
	 * @param aName name.
	 */
	public void setName(String aName) {
		this.name = aName;
	}

	/**
	 * @return start date.
	 */
	public DateTime getStartDate() {
		return startDate;
	}

	/**
	 * @param aStartDate start date.
	 */
	public void setStartDate(DateTime aStartDate) {
		this.startDate = aStartDate;
	}

	/**
	 * @return end date.
	 */
	public DateTime getEndDate() {
		return endDate;
	}

	/**
	 * @param anEndDate end date.
	 */
	public void setEndDate(DateTime anEndDate) {
		this.endDate = anEndDate;
	}

	/**
	 * @return if evaluation session is closed.
	 */
	public Boolean getClosed() {
		return closed;
	}

	/**
	 * @param isClosed if evaluation session is closed.
	 */
	public void setClosed(Boolean isClosed) {
		this.closed = isClosed;
	}

	/**
	 * @return evaluations.
	 */
	public List<Evaluation> getEvaluations() {
		return evaluations;
	}

	/**
	 * @param evaluationList evaluations.
	 */
	public void setEvaluations(List<Evaluation> evaluationList) {
		this.evaluations = evaluationList;
	}

	/**
	 * Returns the evaluation for the evaluee.
	 * 
	 * @param evalueeId the id of the evaluee
	 * @return the selected evaluation
	 */
	public Evaluation getEvaluationForEvaluee(Long evalueeId) {
		Evaluation evaluation = null;
		for (Evaluation eval : evaluations) {
			if (eval.getEvaluee().getProfileId().equals(evalueeId)) {
				evaluation = eval;
				break;
			}
		}

		return evaluation;
	}

	public Boolean getLocked() {
		Boolean isLocked = Boolean.FALSE;

		if (CollectionUtils.isNotEmpty(evaluations)) {
			for (Evaluation evaluation : evaluations) {
				isLocked = evaluation.getPublished();

				if (isLocked) {
					break;
				}
			}
		}

		return isLocked;
	}

	/**
	 * @see com.levi9.hrm.model.base.AbstractBaseEntity#getExcludedAttributes()
	 */
	@Override
	protected String[] getExcludedAttributes() {
		return new String[] { "evaluations" };
	}

}
